﻿using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Models;
using Estsh.Core.Quartz.BaseService;
using Estsh.Core.Util;
using Estsh.Core.Wms.IRepositories;

namespace Estsh.Core.Wms.Jobs
{
    /// <summary>
    /// 大件物料拉动根据预测的JIT信息自动产生拉动单JOB
    /// </summary>
    public class EarlyJitPullJob : IJobService
    {
        private readonly IPullingRepository pullRepository;
        private readonly IMoveOrderRepository moveOrderRepository;



        public EarlyJitPullJob(IPullingRepository _pullGroupRepository,
            IMoveOrderRepository _moveOrderRepository)
        {
            this.pullRepository = _pullGroupRepository;
            this.moveOrderRepository = _moveOrderRepository;

        }

        public string ExecuteService(string parameter)
        {
            try
            {
                List<WmsPullingGroup> pullingGroups = pullRepository.GetPullingGroupList((int)WmsEnumUtil.PullGroupType.EARLY_JIT);
                if (pullingGroups.Count <= 0)
                {
                    return "没有维护拉动组！";
                }

                foreach (var pullingGroup in pullingGroups)
                {
                    List<WmsMoveDetail> moveDetails = pullRepository.GetPullingPartList(pullingGroup.GroupCode, (int)MesEnumUtil.WorkOrderType.EARLY_JIT);
                    if (moveDetails.Count <= 0)
                    {
                        continue;
                    }

                    moveOrderRepository.CreatePullOrder("0", moveDetails);
                }
                return "生成拉动单成功！";

            }
            catch (Exception ex) 
            {
                LogHelper.Error("生成拉动单错误！" + ex);
                return "生成拉动单错误！" + ex.Message;
            }
            
        }
    }
}
